/*
 * 涉及相关文件：
 * /etc/hosts, /etc/services, /etc/gai.conf
 */

/* man getaddrinfo */
/* Server */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>

/* http://blog.sina.com.cn/s/blog_46d93f190100ojap.html */
#define BUF_SIZE 500    /* 这个长度是有讲究的 */

/* gcc section-16.3.3-server.c */
int
main(int argc, char *argv[])
{
    struct addrinfo          hints;
    struct addrinfo         *result, *rp;
    int                      sfd, s;
    struct sockaddr_storage  peer_addr;
    socklen_t                peer_addr_len;
    ssize_t                  nread;
    char                     buf[BUF_SIZE];

    if (argc != 2) {
        fprintf(stderr, "Usage: %s port\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
    hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
    hints.ai_protocol = 0;          /* Any protocol */
    hints.ai_canonname = NULL;
    hints.ai_addr = NULL;
    hints.ai_next = NULL;

    s = getaddrinfo(NULL, argv[1], &hints, &result);
    if (s != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
        exit(EXIT_FAILURE);
    }

    /*
     * getaddrinfo() return a list of address structures.
     * Try each address util we successfully bind(2).
     * if socket(2) (or bind(2)) fails, we (close the sockert
     * and) try the next address.
     */

    for (rp = result; rp != NULL; rp = rp->ai_next) {
        sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (sfd == -1)
            continue;

        if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
            break;      /* Success */

        close(sfd);
    }

    if (rp == NULL) {       /* No address successed */
        fprintf(stderr, "Could not bind\n");
        exit(EXIT_FAILURE);
    }

    freeaddrinfo(result);   /* No longer needed */

    /* Read datagram and echo them back to sender */

    for (;;) {
        peer_addr_len = sizeof(struct sockaddr_storage);
        nread = recvfrom(sfd, buf, BUF_SIZE, 0,
                (struct sockaddr *) &peer_addr, &peer_addr_len);
        if (nread == -1)
            continue;       /* Ignore failed request */

        char host[NI_MAXHOST], service[NI_MAXSERV];

        s = getnameinfo((struct sockaddr *) &peer_addr,
                        peer_addr_len, host, NI_MAXHOST,
                        service, NI_MAXSERV, NI_NUMERICSERV);
        if (s == 0)
            printf("Received %ld bytes from %s:%s\n",
                    (long)nread, host, service);
        else
            fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));

        if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr,
                   peer_addr_len) != nread)
            fprintf(stderr, "Error sending response\n");
    }
}

/*
 * 参考资料：
 * http://blog.chinaunix.net/uid-20196318-id-172427.html
 * 如果你的应用程序需要使用主机名代替IP地址，或服务名代替端口号，
 * 你需要先把对应关系增加到对应的配置文件中，否则getaddrinfo会解析出错.
 */
